package sdp.group14.management.tenant;

import java.text.SimpleDateFormat;
import java.util.Date;

import sdp.group14.R;
import sdp.group14.appdata.Request;
import sdp.group14.management.RequestManagerActivity;
import sdp.group14.management.HousingManagerApplication;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class TenantEditRequestActivity extends RequestManagerActivity {
	
	private EditText commentEditText, descripEditText, causeEditText;
	private CheckBox allowEnter;
	private boolean changesPending;
	private AlertDialog unsavedChangesDialog;
	public static Spinner spinner;
	private ViewFlipper flipper;
	private TextView dateTextView;
	SimpleDateFormat currentTime;
	private Request request;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tenant_edit_request);
       
        flipper = (ViewFlipper) findViewById(R.id.tenant_edit_viewFlipper);
        spinner = (Spinner) findViewById(R.id.tenant_edit_spinner);
		ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.item, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
        
        setUpViews();
    }
	
	protected void updateRequest() {	
		currentTime = new SimpleDateFormat("dd/MM/yyyy/ HH:mm:ss");
		String date = currentTime.format(new Date());
		String item = "" + spinner.getSelectedItem();		
		String status = "new";

		String comment = commentEditText.getText().toString();
		int isAllowEnter = (allowEnter.isChecked()) ? 1 : 0; 
		String descripion = descripEditText.getText().toString();
		String cause = causeEditText.getText().toString();
		Request r = new Request(date, status, item, descripion, comment, cause, isAllowEnter);
		r.setReqID(request.getReqID());
		((HousingManagerApplication)getApplication()).updateRequest(r);
		
		Intent intent = new Intent(TenantEditRequestActivity.this,
				TenantViewRequestActivity.class);
		
		intent.putExtra("request", r);
		startActivity(intent);
	}
	
	protected void cancel() {
		if (changesPending) {
			unsavedChangesDialog = new AlertDialog.Builder(this)
				.setTitle(R.string.unsaved_changes_title)
				.setMessage(R.string.unsaved_changes_message)
				.setPositiveButton(R.string.update_request, new AlertDialog.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						updateRequest();
					}
				})
				.setNeutralButton(R.string.discard, new AlertDialog.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				})
				.setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						unsavedChangesDialog.cancel();
					}
				})
				.create();
			unsavedChangesDialog.show();
		} else {
			finish();
		}
	}

	private void setUpViews() {
		request = (Request)this.getIntent().getSerializableExtra("request");		
		
		commentEditText = (EditText)findViewById(R.id.tenant_edit_comment);
		commentEditText.setText(request.getreqComment());
		spinner = (Spinner) findViewById(R.id.tenant_edit_spinner);
		spinner.setSelection(getPositionByItem(request.getreqCategory()));
		descripEditText = (EditText) findViewById(R.id.tenant_edit_description);
		descripEditText.setText(request.getreqDescription());
		causeEditText = (EditText)findViewById(R.id.tenant_edit_cause);
		causeEditText.setText(request.getreqCause());
		allowEnter = (CheckBox) findViewById(R.id.tenant_edit_checkbx_allowEnter);
		allowEnter.setChecked(request.isReqAllowEnter() == 1);
		dateTextView = (TextView) findViewById(R.id.tenant_edit_text_date);
		dateTextView.setText(request.getreqDate());
		
		commentEditText.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				changesPending = true;
			}
			public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
			public void afterTextChanged(Editable s) { }
		});
		descripEditText.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				changesPending = true;
			}
			public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
			public void afterTextChanged(Editable s) { }
		});
		causeEditText.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				changesPending = true;
			}
			public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
			public void afterTextChanged(Editable s) { }
		});
	}	
	
	private int getPositionByItem(String spinnerItem)
	{
		for(int i = 0; i < spinner.getCount(); i++)
		{
			if(spinnerItem.equals(spinner.getItemAtPosition(i)))
			{
				return i;
			}
		}
		return -1;
	}
    
	/*
	 * Next button is clicked
	 */
    public void onClickNext(View view) {
    	flipper.showNext();
    }
    
    /*
	 * Previous button is clicked
	 */
    public void onClickPrevious(View view) {
    	flipper.showPrevious();
    }
    
    /*
	 * Cancel button is clicked
	 */
    public void onClickCancel(View view) {
    	cancel();
    }
    
    /*
	 * Add task button is clicked
	 */
    public void onUpdateClick(View view) {
    	updateRequest();
    }
    	
    
}
